import logging
import os, django
import time

from UserAPP.models import MS_USER
from shareAPI2020TB.settings import log_wade
from tbitem.JD_utils.wade_Tools_Miao_FaTie4 import Tools_Miaokouling, Tools_QiBo, Tools_BBS, Tools_BaseWeb
from tbitem.JD_utils.wade_Spider_Tool2 import JDSpider
from tbitem.JD_utils.wade_TemplateTool3 import Tools_Template

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "shareAPI2020TB.settings")# project_name 项目名称
django.setup()
from tbitem import models

class jd_run():
    """初始化initjd是初始化各种对象 都需要什么对象呢
    """
    def __init__(self,userOBJ):  #userOBJ是request的登录对象
        self.userOBJ = userOBJ
        # 爬虫类初始化
        self.commentList, self.imgList = [], []
        self.title = ""
        self.category_id = ""

        # 初始化，获取喵口令数据库 配置
        self.miaoAccount_Query = models.WadeMiaoKouLing.objects.filter(pk=self.userOBJ.ms_user_miaokouling_id).all().first()
        if not self.miaoAccount_Query: models.WadeMiaoKouLing.objects.get_or_create(id=0)
        self.tools_mkl_obj = Tools_Miaokouling(self.miaoAccount_Query)  #初始化 喵口令工具类对象
        self.toos_web_obj = Tools_BaseWeb()                             #初始化 发帖类对象
        #初始化结束
        print("账户初始化成功")
    def initBySkuID(self,SkuID):  #利用喵口令工具api，通过id获取所需的内容
        # 构建商品链接，利用喵口令工具类函数获取内容
        itemURL =f"https://item.jd.com/{SkuID}.html"
        self.shotUrl, self.longUrl = self.tools_mkl_obj.getLongUrl_By_FakeUrl(itemURL)
        self.jdid, self.shopid, self.shopname, self.skuName, self.item_MainImg, self.item_price = self.tools_mkl_obj.getjdid(self.shotUrl)
        log_wade.error(msg="喵口令api成功结束")
    def qibo(self):

        # 模板类初始化
        tools_temp_obj = Tools_Template(self.dbdata.title, self.commentList, self.imgList, self.shotUrl, self.shotUrl)
        content = tools_temp_obj.templateFactory_forQb(self.jdid, self.shopid, self.shopname, self.skuName, self.item_MainImg, self.item_price, self.shotUrl,self.longUrl)

        # 发帖
        result = self.toos_qibo_obj.postQiBo(title=self.dbdata.title, content=content)
    def bbs(self):

        #初始化模板
        tools_temp_obj = Tools_Template(self.dbdata.title, self.commentList, self.imgList, self.shotUrl, self.shotUrl)
        content, pinglunList = tools_temp_obj.templateFactory_forDiscuz(self.jdid, self.shopid, self.shopname, self.skuName, self.item_MainImg, self.item_price, self.shotUrl,self.longUrl)
        # 发帖
        result = self.toos_bbs_obj.postBBS(self.dbdata.title,content=content,pinglunList=pinglunList)
        logging.warning(msg=self.dbdata.title)
        # if result == True:
        #     self.dbdata.hasPOst = True
        #     self.dbdata.save()
        # return True
    def web(self):
        try:
        # 处理本站发帖的主逻辑
            log_wade.warning(msg="进入本站发帖模块")
            # 生成模板
            tools_temp_obj = Tools_Template(self.title, self.commentList, self.imgList, self.longUrl, self.longUrl)
            content = tools_temp_obj.templateFactory_forWeb(jdid=self.jdid, shopid=self.shopid, shopname=self.shopname, skuName=self.skuName, item_MainImg=self.item_MainImg, item_price=self.item_price, shotUrl=self.shotUrl,longUrl=self.longUrl)
            log_wade.warning(msg="加载模板 OK")

        # 发帖
            self.toos_web_obj.postBaseWeb(title=self.title, content=content,category_id=self.category_id)
            log_wade.warning(msg="进入本站发帖模块OK")
        except Exception as e:
            log_wade.error(msg=e)

if __name__ == '__main__':
    userObj = MS_USER.objects.all().first()  # 获取用户对象，用户对象关联着喵口令

    jd = jd_run(userOBJ=userObj)  #获取主运行类对象
    jd.imgList =['']
    jd.commentList =['']
    jd.initBySkuID(100005404692)
    jd.web()

